package autocode

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/autocode"
	autoCodeReq "github.com/flipped-aurora/gin-vue-admin/server/model/autocode/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
)

type ConstantInfoService struct {
}

// CreateConstantInfo 创建ConstantInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (constantInfoService *ConstantInfoService) CreateConstantInfo(constantInfo autocode.ConstantInfo) (err error) {
	err = global.GVA_DB.Create(&constantInfo).Error
	return err
}

// DeleteConstantInfo 删除ConstantInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (constantInfoService *ConstantInfoService)DeleteConstantInfo(constantInfo autocode.ConstantInfo) (err error) {
	err = global.GVA_DB.Delete(&constantInfo).Error
	return err
}

// DeleteConstantInfoByIds 批量删除ConstantInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (constantInfoService *ConstantInfoService)DeleteConstantInfoByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]autocode.ConstantInfo{},"id in ?",ids.Ids).Error
	return err
}

// UpdateConstantInfo 更新ConstantInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (constantInfoService *ConstantInfoService)UpdateConstantInfo(constantInfo autocode.ConstantInfo) (err error) {
	err = global.GVA_DB.Save(&constantInfo).Error
	return err
}

// GetConstantInfo 根据id获取ConstantInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (constantInfoService *ConstantInfoService)GetConstantInfo(id uint) (err error, constantInfo autocode.ConstantInfo) {
	err = global.GVA_DB.Where("id = ?", id).First(&constantInfo).Error
	return
}

// GetConstantInfoInfoList 分页获取ConstantInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (constantInfoService *ConstantInfoService)GetConstantInfoInfoList(info autoCodeReq.ConstantInfoSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&autocode.ConstantInfo{}).Order("created_at desc")
    var constantInfos []autocode.ConstantInfo
    // 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }
	err = db.Limit(limit).Offset(offset).Find(&constantInfos).Error
	return err, constantInfos, total
}
